package com.service.infoEntry.service.impl;

import com.service.config.utils.DateUtils;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import com.service.infoEntry.dao.CommentDao;
import com.service.infoEntry.dao.ProgrammeCommentDao;
import com.service.infoEntry.dto.CommentDto;
import com.service.infoEntry.dto.ProgrammeCommentDto;
import com.service.infoEntry.entity.CommentInfo;
import com.service.infoEntry.entity.ProgrammeCommentInfo;
import com.service.infoEntry.service.CommentService;
import com.service.infoEntry.service.ProgrammeCommentService;
import com.service.infoEntry.service.UserInfoService;
import com.service.infoEntry.vo.ProgrammeCommentInfoVo;
import com.service.infoEntry.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProgrammeCommentServiceImpl implements ProgrammeCommentService {
    @Resource
    private ProgrammeCommentDao programmeCommentDao;
    @Resource
    private UserInfoService userService;

    @Override
    public void addComment(ProgrammeCommentDto programmeCommentDto) {
        ProgrammeCommentInfo commentInfo = ModelMapperUtil.strictMap(programmeCommentDto, ProgrammeCommentInfo.class);
        commentInfo.setId(IdUtil.getStringId());
        commentInfo.setAddTime(DateUtils.getNowDate());
        UserVo userVo = userService.queryByUserId(programmeCommentDto.getUserId());
        if(null != userVo)
        {
            commentInfo.setUsername(userVo.getUsername());
            programmeCommentDao.addComment(commentInfo);
        }
    }

    @Override
    public List<ProgrammeCommentInfoVo> queryAllCommentByProgrammeId(String programmeId,String userId) {
        List<ProgrammeCommentInfoVo> programmeCommentInfoVos = new ArrayList<>();
        ProgrammeCommentInfoVo programmeCommentInfoVo;
        UserVo userVo;
        List<ProgrammeCommentInfo> programmeCommentInfos = programmeCommentDao.queryAllCommentByProgrammeId(programmeId);
        if(null !=programmeCommentInfos || programmeCommentInfos.size()>=0)
        {
            for (ProgrammeCommentInfo programmeCommentInfo : programmeCommentInfos) {
                programmeCommentInfoVo = ModelMapperUtil.strictMap(programmeCommentInfo,ProgrammeCommentInfoVo.class);
                userVo = userService.queryByUserId(programmeCommentInfo.getUserId());
                if(programmeCommentInfo.getUserId().equals(userId))
                {
                    userVo.setCurrentUser(true);
                }
                if (userVo.getFullName() == null) {
                    userVo.setFullName("");
                }

                programmeCommentInfoVo.setUserVo(userVo);
                programmeCommentInfoVos.add(programmeCommentInfoVo);
            }
        }
        return programmeCommentInfoVos;
    }

    @Override
    public void deleteCommentByCommentId(String commentId) {
        programmeCommentDao.deleteCommentByCommentId(commentId);
    }
}
